# -*- coding: utf-8 -*-
# https://blog.csdn.net/weixin_52040868
# 公众号：测个der
# 微信：qing_an_an


import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class Main_(QWidget):
    def __init__(self):
        super(Main_, self).__init__()

        self.thread = Work()
        self.listFile = QListWidget()
        self.btnStart = QPushButton("开始")

        layout = QGridLayout()
        layout.addWidget(self.listFile,0,0,1,2)
        layout.addWidget(self.btnStart,1,0,1,2)
        self.btnStart.clicked.connect(self.slotStart)
        self.thread.sinOut.connect(self.slotAdd)

        self.setLayout(layout)

    def slotStart(self):
        # 设置开始按钮不可点击，结束按钮可点击
        self.btnStart.setEnabled(False)
        self.thread.start()


    def slotAdd(self,file_inf):
        self.listFile.addItem(file_inf)
        if '5' in file_inf:
            self.close()

class Work(QThread):
    # 自定义信号
    sinOut = pyqtSignal(str)

    def __init__(self):
        super(Work, self).__init__()
        self.working = True
        self.num = 0

    def __del__(self):
        self.working = False
        self.wait()

    def run(self):
        while self.working == True:
            file_str = "File index {}".format(self.num)
            self.num += 1
            self.sinOut.emit(file_str)
            self.sleep(2)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_ = Main_()
    main_.show()
    sys.exit(app.exec_())